<template>
  <el-dialog
    :visible="showDialog"
    :title="showTitle"
    @close="closeHandler"
  >
    <el-form
      ref="deptForm"
      label-width="140px"
      :model="formdata"
      :rules="rules"
    >
      <el-form-item
        prop="name"
        label="部门名称"
      >
        <el-input
          v-model="formdata.name"
          placeholder="长度为1-50个字符"
        />
      </el-form-item>
      <el-form-item
        prop="code"
        label="部门编码"
      >
        <el-input
          v-model="formdata.code"
          placeholder="长度为1-50个字符"
        />
      </el-form-item>
      <el-form-item
        prop="manager"
        label="部门负责人"
      >
        <el-select
          v-model="formdata.manager"
          placeholder="请选择"
          @focus="getEmployeeSimple"
        >
          <el-option
            v-for="item in employeeList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        prop="introduce"
        label="介绍"
      >
        <el-input
          v-model="formdata.introduce"
          placeholder="长度为1-300个字符"
        />
      </el-form-item>
    </el-form>
    <el-row
      slot="footer"
      type="flex"
      justify="center"
    >
      <el-button
        size="small"
        @click="closeHandler"
      >取消</el-button>
      <el-button
        type="primary"
        @click="btnOK"
      >确 定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimpleList } from '@/api/employees'
import { getDepartments, addDepartment, updateDepartments, getDepartDetail } from '@/api/departments'
export default {
  name: '',
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data () {
    const checkedName = async (rules, value, callback) => {
      const { depts } = await getDepartments()
      let bool = false
      if (this.formdata.id) {
        bool = depts.filter(item => item.id !== this.formdata.id && item.pid === this.treeNode.id).some(item => item.name === value)
      } else {
        bool = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }
      bool ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
    }
    const checkedCode = async (rules, value, callback) => {
      const { depts } = await getDepartments()
      let bool = false
      if (this.formdata.id) {
        bool = depts.some(item => item.name === value && item.id !== this.treeNode.id && value)
      } else {
        bool = depts.some(item => item.code === value && value)
      }
      bool ? callback(new Error(`组织架构中已经有部门使用${value}编码`)) : callback()
    }

    return {
      employeeList: [],
      formdata: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称长度为1-50个字符', trigger: 'blur' },
          { validator: checkedName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称长度为1-50个字符', trigger: 'blur' },
          { validator: checkedCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门名称长度为1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  // 计算属性
  computed: {
    showTitle () {
      return this.formdata.id ? '编辑部门' : '新增子部门'
    }
  },
  // 侦听器
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async getEmployeeSimple () {
      const data = await getEmployeeSimpleList()
      this.employeeList = data
      console.log(data)
    },
    btnOK () {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          if (this.formdata.id) {
            await updateDepartments(this.formdata)
          } else {
            await addDepartment({ ...this.formdata, pid: this.treeNode.id })
          }
          this.$emit('update:showDialog', false)
          this.$emit('updateDeparts')
        }
      })
    },
    closeHandler () {
      this.formdata = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.deptForm.resetFields() // 重置校验字段
      this.$emit('update:showDialog', false) // 关闭
    },
    // 在add-dept.vue中定义getDepartDetail方法
    // 获取部门详情
    async getDepartDetail (id) {
      this.formdata = await getDepartDetail(id)
      this.$nextTick(() => {
        console.log(this.formdata)
      })
    }
  }
}
</script>

<style scoped lang="less">
</style>
